package com.ruoyi.market.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.market.domain.Order;
import com.ruoyi.market.domain.OrderDetail;
import com.ruoyi.market.mapper.OrderMapper;
import com.ruoyi.market.service.OrderDetailService;
import com.ruoyi.market.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 *
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailService detailService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteOrder(String orderIndent) {
        boolean a=false;
        try {

            a=orderMapper.deleteOrder(orderIndent);
            boolean b=detailService.deleteByMkOrderIndent(orderIndent);
            if (!b){
                throw new Exception("删除订单详情表失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //@Transactional和try catch捕获异常会让注解失效
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return a;
    }

    @Override
    public List<Order> queryList(Order order) {
        return orderMapper.queryList(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addOrder(Order order, List<OrderDetail> orderDetailList) {
        boolean a=false;
        try {
            a=orderMapper.addOrder(order);
            boolean b=detailService.addDetails(orderDetailList);
            if (!b){
                throw new Exception("新增订单详情表失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //@Transactional和try catch捕获异常会让注解失效
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return a;
    }

    @Override
    public Order getByOrderIndent(String orderIndent) {
        return orderMapper.getByOrderIndent(orderIndent);
    }

    @Override
    public boolean modify(Order order) {
        return orderMapper.modify(order);
    }


}




